﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eShop.DataAccess.Converter
{
    public class ShippingRateConverter
    {
        public static void AttachAssociation(List<Model.ShippingRate> models, List<Model.MetaData.City> associations)
        {
            foreach (Model.ShippingRate model in models)
            {
                model.ToCity = associations.Single(c => c.Id == model.ToCity.Id);
            }
        }

        public static List<Model.ShippingRate> ConvertToModelObject(List<Entity.ShippingRate> entities)
        {
            List<Model.ShippingRate> models = new List<Model.ShippingRate>(entities.Count);
            foreach (Entity.ShippingRate entity in entities)
            {
                models.Add(ConvertToModelObject(entity));
            }
            return models;
        }

        public static Model.ShippingRate ConvertToModelObject(Entity.ShippingRate entity)
        {
            if (entity == null) return null;
            Model.ShippingRate model = new Model.ShippingRate();
            model.Id = entity.Id;
            model.ToCity = new Model.MetaData.City() { Id = entity.CityId };
            model.FromWeight = (float)entity.StartingWeight;
            model.ToWeight = (float)entity.EndingWeight;
            model.Rate = (float)entity.Rate;
            return model;
        }

        public static Entity.ShippingRate ConvertToEntity(Model.ShippingRate model)
        {
            Entity.ShippingRate entity = new Entity.ShippingRate();
            entity.Id = model.Id;
            entity.CityId = model.ToCity.Id;
            entity.StartingWeight = (double)model.FromWeight;
            entity.EndingWeight = (double)model.ToWeight;
            entity.Rate = (double)model.Rate;
            return entity;
        }
    }
}
